package com.yin.myproject.thread.pcdesign;

import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.BlockingQueue;

public class Consumer implements Runnable {
    private BlockingQueue<PCData> queue;
    private static final int SLEEP_TIME = 1000;
    private volatile boolean isRunning = true;

    public Consumer(BlockingQueue<PCData> queue) {
        this.queue = queue;
    }


    @Override
    public void run() {
        System.out.println("start Consumer id = " + Thread.currentThread().getId());
        Random random = new Random();
        try {
            while (isRunning) {
                PCData pcData = queue.take();
                if (null != pcData) {
                    int re = pcData.getIntData() * pcData.getIntData();
                    System.out.println(MessageFormat.format("{0}*{1}={2}", pcData.getIntData(), pcData.getIntData(), re));
                    Thread.sleep(random.nextInt(SLEEP_TIME));
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
